"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 文件目录
Case Name   : 配置文件丢失恢复后，备机重建auto模式
Create At   : 2020/07/24
Owner       : @li-xin12345
Description :
    1.备机删除配置文件
    2.备机重建
    3.恢复配置文件
    4.关闭数据库
    5.查询NextXID和NextOID
    6.备机清理xlog
    7. 删除pg_xlog
    8. 启动数据库
    9.备机重建
    10.查看主备状态
Expect      :
History     :
    Modified by @wan005 at 2021/09/27:优化步骤5+6增加source
"""

import unittest

from testcase.utils.CommonSH import *
from testcase.utils.CommonSH import *
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

logger = Logger()


class IndexFileDamaged(unittest.TestCase):
    sh_primy = CommonSH('PrimaryDbUser')
    sh_stadb = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        logger.info('------------Opengauss_Reliability_Dbsys_Case177.py start------------')
        self.rootNode = Node('PrimaryRoot')
        self.primaryNode = Node(node='PrimaryDbUser')
        self.standbyRootNode = Node('Standby1Root')
        self.standbyNode = Node(node='Standby1DbUser')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.DB_MY_PATH = macro.DB_BACKUP_PATH
        self.Constant = Constant()

    def test_Index_file_damaged(self):
        # 备机删除配置文件
        logger.info('-----------------------备机移除配置文件---------------------------')
        excute_cmd = f'mv {self.DB_INSTANCE_PATH}/{macro.DB_PG_CONFIG_NAME} {self.DB_MY_PATH}/'
        logger.info(excute_cmd)
        msg = self.standbyNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertNotIn(self.Constant.SQL_WRONG_MSG[1], msg)

        logger.info('-----------------------进行备机重建-----------------------------------')
        buildMsg = self.sh_stadb.executeGsctl('build', self.Constant.GS_CTL_STOP_SUCCESS_MSG, '-b auto')
        logger.info(buildMsg)
        self.assertTrue(buildMsg)

        logger.info('-----------------------恢复配置文件---------------------------')
        excute_cmd = f'mv {self.DB_MY_PATH}/{macro.DB_PG_CONFIG_NAME} {self.DB_INSTANCE_PATH}/'
        logger.info(excute_cmd)
        msg = self.standbyNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertNotIn(self.Constant.SQL_WRONG_MSG[1], msg)

        logger.info('-----------------------关闭数据库---------------------------')
        excute_cmd = f'''
                        source {self.DB_ENV_PATH};
                        gs_om -t stop;'''
        logger.info(excute_cmd)
        msg = self.standbyNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertNotIn(self.Constant.SQL_WRONG_MSG[1], msg)

        logger.info('-----------------------查询NextXID和NextOID---------------------------')
        excute_cmd = f'source {self.DB_ENV_PATH};' \
                     f'pg_controldata {self.DB_INSTANCE_PATH}'
        logger.info(excute_cmd)
        msg = self.standbyNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertNotIn(self.Constant.SQL_WRONG_MSG[1], msg)
        self.assertNotIn("bash", msg)

        logger.info('-----------------------备机删除pg_xlog---------------------------')
        excute_cmd = f'source {self.DB_ENV_PATH};' \
                     f'pg_resetxlog -o NextOID -x NextXID -f {self.DB_INSTANCE_PATH}/'
        logger.info(excute_cmd)
        msg = self.standbyNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertNotIn(self.Constant.SQL_WRONG_MSG[1], msg)
        self.assertNotIn("bash", msg)

        logger.info('-----------------------启动数据库---------------------------')
        excute_cmd = f'''
                        source {self.DB_ENV_PATH};
                        gs_om -t start;'''
        logger.info(excute_cmd)
        msg = self.standbyNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertIn(self.Constant.START_SUCCESS_MSG, msg)

        logger.info('-----------------------再次进行备机重建-----------------------------------')
        buildMsg = self.sh_stadb.executeGsctl('build', self.Constant.REBUILD_SUCCESS_MSG, '-b auto')
        logger.info(buildMsg)
        self.assertTrue(buildMsg)

        logger.info('-----------------------查看主备状态---------------------------')
        excute_cmd = f'''
                        source {self.DB_ENV_PATH};
                        gs_om -t status --detail;'''
        logger.info(excute_cmd)
        msg = self.standbyNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertIn(self.Constant.START_STATUS_MSG, msg)

    def tearDown(self):
        logger.info('----------------------------后置处理-----------------------------')
        try:
            db_status = self.sh_primy.getDbInstanceStatus()
            logger.info(db_status)
            if db_status:
                pass
            else:
                is_started = self.sh_primy.startDbCluster()
                self.assertTrue(is_started)
                logger.info(f'{is_started}')
                logger.info(f'db_status: {is_started}')
        finally:
            excute_cmd = f'mv {self.DB_MY_PATH}/{macro.DB_PG_CONFIG_NAME} {self.DB_INSTANCE_PATH}/'
            logger.info(excute_cmd)
            msg = self.standbyNode.sh(excute_cmd).result()
            logger.info(msg)
        logger.info('----------------------------Reliability_Dbsys_Case177执行完成-----------------------------')
